/*
 *   Copyright (c) 2010 Sonrisa Informatikai Kft. All Rights Reserved.
 * 
 *  This software is the confidential and proprietary information of
 *  Sonrisa Informatikai Kft. ("Confidential Information").
 *  You shall not disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Sonrisa.
 * 
 *  SONRISA MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 *  THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 *  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SONRISA SHALL NOT BE LIABLE FOR
 *  ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 *  DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package com.zh.jbet.web.panels.teammatch;

import com.zh.jbet.web.panels.teammatch.TeamMatchEditModalPanel;
import com.zh.jbet.core.entity.GameContext;
import com.zh.jbet.core.tournament.entity.TeamMatch;
import com.zh.jbet.web.JBetServiceProvider;
import com.zh.jbet.web.components.datatable.JBetEditDataPanel;
import com.zh.jbet.web.components.datatable.TableColumnDescriptor;
import com.zh.jbet.web.model.IServiceableModel;
import com.zh.jbet.web.pages.BasePanel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.tree.BaseTree;
import org.apache.wicket.markup.html.tree.LinkTree;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author sonrisa
 */
public class ContextTreeMatchPanel extends BasePanel {
    private static final TableColumnDescriptor[] COLS = new TableColumnDescriptor[] {
        new TableColumnDescriptor(TableColumnDescriptor.ColumnType.DATE_PROPERTY, "startTime", true),
        new TableColumnDescriptor(TableColumnDescriptor.ColumnType.FILTERED, "home.name", true),
        new TableColumnDescriptor(TableColumnDescriptor.ColumnType.FILTERED, "away.name", true),
        new TableColumnDescriptor(TableColumnDescriptor.ColumnType.PROPERTY, "resultString", true)
    };

    private JBetEditDataPanel<TeamMatch> editPanel;
    private LinkTree ltree;

    public ContextTreeMatchPanel(String id, final IModel<TreeModel> model) {
        super(id, model);
        ltree = new LinkTree("tree", model) {
            @Override
            protected IModel getNodeTextModel(IModel<Object> tmodel) {
                return new PropertyModel(tmodel, "userObject.name");
            }

            @Override
            protected void onNodeLinkClicked(Object onode, BaseTree btree, AjaxRequestTarget target) {
                btree.getTreeState().selectNode(onode, true);
                target.addComponent(getFeedback());
                selectedChanged(target);
            }

        };
        ltree.getTreeState().expandAll();
        ltree.getTreeState().setAllowSelectMultiple(false);
        ltree.getTreeState().selectNode(model.getObject().getRoot(), true);
        add(ltree);

        add(editPanel = new JBetEditDataPanel<TeamMatch>("editPanel", 10, JBetServiceProvider.MATCH_SERVICE, COLS, TeamMatchEditModalPanel.class) {
            @Override
            protected IServiceableModel<TeamMatch> createServiceableModel(IModel<TeamMatch> model) {
                IServiceableModel<TeamMatch> smodel = super.createServiceableModel(model);
                GameContext gc = getSelectedContext();
                smodel.getObject().setParentId(gc.getId());
                smodel.getObject().setRootId(gc.getRootId() != null ? gc.getRootId() : gc.getId());
                return smodel;
            }
        });
        //providerFilter.setRootId(getSelectedGame().getId());
    }

    private void selectedChanged(AjaxRequestTarget target) {
        editPanel.getDataProvider().getFilterState().setParentId(getSelectedContext().getId());
        if (target != null)
            target.addComponent(editPanel);
    }

    protected GameContext getSelectedContext() {
        DefaultMutableTreeNode selected = (DefaultMutableTreeNode)ltree.getTreeState().getSelectedNodes().iterator().next();
        return (GameContext)selected.getUserObject();
    }
}
